package com.huaxiafinance.management.util;


import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import java.io.StringWriter;
import java.util.Map;


/**
 * Created by win7 on 2017/9/13.
 */
public class VelocityUtil {
    /**
     * 获取短信模板
     *
     * @param model
     * @param paramMap
     * @return String
     */
    public static String getSmsModel(String model, Map<String, String> paramMap)
    {
        // 初始化并取得Velocity引擎
        VelocityEngine ve = new VelocityEngine();
        ve.init();
        // 取得velocity的模版内容, 模板内容来自字符传
        String content = model;
        // 取得velocity的上下文context
        VelocityContext context = new VelocityContext();
        // 把数据填入上下文
        if (paramMap != null)
        {
            paramMap.forEach((k, v) -> {
                context.put(k, v);
            });
        }

        // 输出流
        StringWriter writer = new StringWriter();
        // 转换输出
        ve.evaluate(context, writer, "", content); // 关键方法

        return writer.toString();
    }
}
